package main;

import BuyerAgent.Buyer;
import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.wrapper.AgentController;
//import org.jvnet.substance.SubstanceLookAndFeel;
//import org.jvnet.substance.theme.SubstanceLightAquaTheme;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: jiaka_000
 * Date: 13-5-16
 * Time: 3:55
 * backup gui launcher for the system
 */
public class GUILauncher extends JFrame {

    public static final String FRUIT_TREE_SYSTEM_GUI_LAUNCHER = "Fruit Tree System GUI Launcher";
    public static final String START = "Start";
    public static final String WORKING_MODE = "Working Mode";
    public static final String TESTING_MODE = "Testing Mode";
    public static final String NUMBER_OF_BUYER = "Number of Buyer:";
    public static final String NUMBER_OF_SELLER = " Number of Seller:";
    public static final int BORDER = 5;
    public static final int ROWS = 2;
    public static final int COLS = 4;
    public static final String NOTICE = "Notice";
    public static final String THE_FRUIT_TREE_SYSTEM_IS_LAUNCHING_PLEASE_WAIT = "The fruit tree system is launching. Please wait";
    private Integer numberOfSeller = 1;
    private Integer numberOfBuyer = 1;
    public static final String NO_GUI = "No GUI";
    private JRadioButton jrb1;
    private JRadioButton jrb2;
    private JTextField numberOfBuyerTextBox;
    private JTextField numberOfSellerTextBox;

    public GUILauncher(){

        //setUIStyle();
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            //e.printStackTrace();
        }

        setTitle(FRUIT_TREE_SYSTEM_GUI_LAUNCHER);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ButtonGroup bg = new ButtonGroup();
        jrb1 = new JRadioButton(WORKING_MODE);
        jrb2 = new JRadioButton(TESTING_MODE);
        bg.add(jrb1);
        bg.add(jrb2);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(ROWS, COLS));
        panel.setBorder(BorderFactory.createEmptyBorder(BORDER, BORDER, BORDER, BORDER));

        panel.add(new JLabel(NUMBER_OF_BUYER));
        numberOfBuyerTextBox = new JTextField(numberOfBuyer.toString());
        panel.add(numberOfBuyerTextBox);

        panel.add(new JLabel(NUMBER_OF_SELLER));
        numberOfSellerTextBox = new JTextField(numberOfSeller.toString());
        panel.add(numberOfSellerTextBox);
        panel.add(new JLabel());//add a empty label to occupy the place

        panel.add(jrb1);
        panel.add(jrb2);
        getContentPane().add(panel, BorderLayout.CENTER);

        JButton btnStart = new JButton(START);
        btnStart.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Launcher launcher=new Launcher();
                launcher.start();

                JOptionPane.showConfirmDialog(
                        GUILauncher.this, THE_FRUIT_TREE_SYSTEM_IS_LAUNCHING_PLEASE_WAIT, NOTICE,JOptionPane.DEFAULT_OPTION);
            }
        });
        panel.add(btnStart);

        panel = new JPanel();
        panel.add(btnStart);
        getContentPane().add(panel, BorderLayout.SOUTH);

    }

    public static void main(String[] args) {
        GUILauncher starter = new GUILauncher();
        starter.showGui();
    }

//    private void setUIStyle() {
//
//        try {
//            UIManager.setLookAndFeel(new SubstanceLookAndFeel());
//            UIManager.put("swing.boldMetal", false);
//            if (System.getProperty("substancelaf.useDecorations") == null) {
//                JFrame.setDefaultLookAndFeelDecorated(true);
//                JDialog.setDefaultLookAndFeelDecorated(true);
//            }
//            System.setProperty("sun.awt.noerasebackground", "true");
//
//            SubstanceLookAndFeel.setCurrentTheme(new SubstanceLightAquaTheme());
//
//
//
//        } catch (Exception e) {
//            System.err.println("Oops!  Something went wrong!");
//        }
//    }

    // using a background thread to start agents
    private class Launcher extends Thread
    {
        public void run ()
        {
            numberOfBuyer = Integer.parseInt(numberOfBuyerTextBox.getText());
            numberOfSeller = Integer.parseInt(numberOfSellerTextBox.getText());

            if (jrb1.isSelected()) {
                startWorkingMode();
            } else if (jrb2.isSelected()) {
                startTestingMode();
            }
        }
    }

    public void showGui() {
        pack();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int centerX = (int) screenSize.getWidth() / 2;
        int centerY = (int) screenSize.getHeight() / 2;
        setLocation(centerX - getWidth() / 2, centerY - getHeight() / 2);

        super.setVisible(true);
    }

    private void startWorkingMode() {

    }

    private void startTestingMode() {
        try {
            // Get a hold on JADE runtime
            jade.core.Runtime rt = Runtime.instance();

            // Exit the JVM when there are no more containers around
            rt.setCloseVM(true);

            //Start Main Container
            Profile pMain = new ProfileImpl(null, 9999, null);
            jade.wrapper.AgentContainer mainContainer = rt.createMainContainer(pMain);
            mainContainer.start();

            //CREATE ALL BUYER AGENTS WHICH ARE INITIALLY LOCATED IN DIFFERENT CONTAINERS
            for (int i = 1; i <= numberOfBuyer; i++) {
                //create container
                Profile p = new ProfileImpl(false);
                p.setParameter(Profile.CONTAINER_NAME, "Container-" + i);
                jade.wrapper.AgentContainer agentContainer = rt.createAgentContainer(p);
                agentContainer.start();
                String buyerAgentName = "BuyerAgent-" + i;
                ArrayList<String> argsList = new ArrayList<String>();
                argsList.add(buyerAgentName);
                //create an instance of buyer agent
                AgentController buyerAgent = agentContainer.createNewAgent(buyerAgentName, Buyer.class.getName(), null);
                buyerAgent.start();
            }

            //start all seller agents in main container
            java.util.List<String> arguments = new ArrayList<String>();
            arguments.add(NO_GUI);
            for (int i = 0; i < numberOfSeller; i++) {
                //AgentController seller = mainContainer.createNewAgent("SellerAgent_" + i, "SellerAgent.Seller", arguments.toArray());
                AgentController seller = mainContainer.createNewAgent("SellerAgent_" + i, "SellerAgent.Seller", null);
                seller.start();
            }

            System.out.println("Launching the rma agent on the main container ...");
            AgentController rma = mainContainer.createNewAgent("rma", "jade.tools.rma.rma", new Object[0]);
            rma.start();

        } catch (Exception e) {
            //e.printStackTrace();
        }
    }


}
